<?php
if (!defined('CMS_SYSTEM')) exit;

if(file_exists(DATAFOLD."/config_surveys.php")) require(DATAFOLD."/config_surveys.php");

global $db, $prefix, $currentlang, $module_name, $Default_Temp;

$content = "";
$content.= "<div class=\"div-block\">";
if($module_name != "surveys") {
	$bl_arr = array();
	$bl_arr[] = $bl_l;
	$bl_arr[] = $bl_r;
	$basename = pathinfo(__FILE__, PATHINFO_BASENAME);
	$correctArr = array();
	for ($i = 0; $i < count($bl_arr); $i++) {
		for ($h = 0; $h < count($bl_arr[$i]); $h++) {
			$temp = explode("@", $bl_arr[$i][$h]);
			if (($temp[5] == $currentlang) && ($temp[6] == $basename)) {
				$correctArr = $temp;
				break;
			}
		}
	}
	
	$content.="<div class=\"div-tblock\">Thăm dò</div>";
	$content .= "<table width=\"100%\">";
	
	$content .= "<tr><td colspan=\"2\"><img src=\"templates/{$Default_Temp}/images/spacer.gif\" height=\"3\" /></td></tr>";
	
	if($svblocktype == 1) {
		$result = $db->sql_query("SELECT id, question, votes, anwsers, options, totalvotes FROM ".$prefix."_survey WHERE active=1 AND alanguage='$currentlang' ORDER BY rand() DESC LIMIT 1");
	} else {
		$result = $db->sql_query("SELECT id, question, votes, anwsers, options, totalvotes FROM ".$prefix."_survey WHERE active=1 AND alanguage='$currentlang' ORDER BY time DESC LIMIT 1");
	}
	if($db->sql_numrows($result) > 0) {
		list($idsv, $titlesv, $votes, $anwsers, $options, $totalvotes) = $db->sql_fetchrow($result);
		$anwsers_ar = @explode("|",$anwsers);
		$votes_ar = @explode("|",$votes);

		echo "<script language=\"javascript\">";
		echo "	function checkVote(vote) {";
		echo "		var vcheck = 0;";
		echo "		var poll = '';";
		echo "		for(var i=0; i < vote.length; i ++) {";
		echo "			if (vote[i].checked == true) {";
		echo "				poll = i;";
		echo "				vcheck = 1;";
		echo "			}	";
		echo "		}";
		echo "		if(vcheck == 0) {";
		echo "			alert('"._PLCHOOSEAS."');";
		echo "			return false;";
		echo "		} else {";
		echo "			openNewWindow('".url_sid("index.php?f=surveys&do=result&id=".$idsv."&vote='+poll,300,550,1").");";
		echo "		}			";
		echo "	}	";
		echo "</script>	";

		$content .= "<form method=\"POST\" action=\"".url_sid("index.php?f=surveys&do=result&id=$idsv")."\">";
		$content .= "<tr><td colspan=\"2\">$titlesv</td></tr>";
		for($i =0; $i < $options; $i ++) {
			$content .= "<tr><td width=\"1%\"><input type=\"radio\" name=\"vote\" value=\"$i\"></td><td>$anwsers_ar[$i]</td></tr>";
		}
		$content .= "<tr><td colspan=\"2\" align=\"center\" style=\"position: relative; left: -2px\"><input type=\"button\" value=\""._VOTE."\" onclick=\"checkVote(vote);\" class=\"sb_but1\"><input type=\"button\" value=\""._RESULT."\" onclick=\"openNewWindow('".url_sid("index.php?f=surveys&do=result&id=$idsv")."',300,550,1)\" class=\"sb_but1\" style=\"position: relative; left: 2px\"></td></tr>";
		$content .= "</form></table>";
		
	}
	
}
$content.="</div>";
?>